#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QSplitter>
#include <QSettings>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pageManager = new PageManager(this);

    connect(ui->actionSplit_Hor, SIGNAL(triggered(bool)), pageManager, SLOT(split_hor()));
    connect(ui->actionSplit_Ver, SIGNAL(triggered(bool)), pageManager, SLOT(split_ver()));

    ui->centralWidget->layout()->addWidget(pageManager);

    m_settings = new QSettings("Config.xml",
                               XmlConfig::XmlSettingsFormat,
                               this);

    readSettings(m_settings);
    pageManager->readSettings(m_settings);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::saveSettings(QSettings* qs)
{
    qs->beginGroup("WindowsSize");
    qs->setValue("geometry", geometry());
    qs->endGroup();
}

void MainWindow::readSettings(QSettings* qs)
{
    qs->beginGroup("WindowsSize");
    const QVariant geom = qs->value("geometry");
    if (geom.isValid()) {
        setGeometry(geom.toRect());
    } else {
        resize(750, 400);
    }
    qs->endGroup();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    pageManager->saveSettings(m_settings);
    saveSettings(m_settings);
    event->accept();
}
